<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>
<%@ page import="org.swim.entity.User" %>
<%@ page import="org.swim.entity.Ability" %>
<%@ page import="org.swim.entity.HelpRequests" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>Profile</title>
	<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
	<%@ include file="NavBar.jsp" %>
	<div class="container-fluid">
	<% 	
	User viewing = (User) request.getAttribute("userProfile");
	User me = (User) session.getAttribute("user");
	String name = viewing.getNome();
	String surname = viewing.getCognome();
	String email = viewing.getEmail();
	boolean admin = viewing.isAdmin();
	boolean mine = (Boolean) request.getAttribute("isMyProfile");
	boolean friend = (Boolean) request.getAttribute("isMyFriend");	
	boolean pendingFriendship = (Boolean) request.getAttribute("pendingFriendship");
	%>
	<h1><% out.print(name + " " + surname); %> <% if(friend==true) { %>
	<span class="label label-success">Friend</span>
	<% } %></h1>	
		<div class="span4">
		<p><img src="<% out.print(viewing.getAvatar()); %>" class="img-polaroid"></p>
		<h3>Name: </h3><p><% out.print(name); %></p>
		<h3>Surname: </h3><p><% out.print(surname); %></p>
		<h3>Email: </h3><p><% out.print(email); %></p>
		<h3>Status: </h3><p><% if(admin==true) { out.print("Administrator"); } else { out.print("User"); } %></p>
		<% if (mine) { %>
		<form method="get" action="EditProfile">
		<input type="hidden" name="profile" value="<% out.print(viewing.getId()); %>" class="search-query" />
		<p><button type="submit" class="btn" type="button">Change Avatar</button></p>
		</form>
		<% } %>
		</div>
	  	<div class="span8">
		<div class="container">
		<% if (!status.equals("guest")) { // Zona riservata agli utenti loggati%>
			<% if (mine==false) {
				if (friend==false && pendingFriendship==false) { %>
					<a href="SendFriendship?u1=<% out.print(me.getId());%>&u2=<% out.print(viewing.getId());%>">
						<button class="btn btn-success" type="button">Send Friend Request</button>
					</a>
					<% } else if (friend==false && pendingFriendship==true) { %>
					<button class="btn btn-warning" type="button">Pending Friend Request</button>						
					<% } else { %>
					<a href="DeleteFriendship?u1=<% out.print(me.getId());%>&u2=<% out.print(viewing.getId());%>">
						<button class="btn btn-danger" type="button">Delete Friend Request</button>
					</a>	
					<a href="SendHelpRequest?user=<% out.print(viewing.getId());%>">
						<button class="btn" type="button">Send Help Request</button>
					</a>					
					<% } %>
						<% if(status.equals("admin")) { %>
							<% if (admin==true) { %>
							  <a href="AdminNominations?user=<% out.print(viewing.getId());%>&action=revoke">
							  <button class="btn btn-danger" type="button">Revoke Admin</button>
							  </a>
							<% } else { %>
							  <a href="AdminNominations?user=<% out.print(viewing.getId());%>&action=promote">
							  <button class="btn btn-success" type="button">Promote Admin</button>
							  </a>
							<% } 
						} 
			} %>
		<% } %></div>
		<div class="container">
		<h3>Abilities: </h3>

		<% List<Ability> abilities = (List<Ability>) request.getAttribute("abilities");
		if (!(abilities.size()==0)) {
		for(Ability current: abilities) { %>
			<p><% out.print(current.getNome()); %></p>
		<% } %>
		<% } else { %>
			<div class="alert alert-info"><strong>No abilities:</strong> this user has no abilities</div>
		<% } %>
		<% if (mine==true) { %>
			<a href="SendAbilityRequest">
			<button class="btn btn-success" type="button">Add an ability</button>
			</a>
		<% } %>		
		</div>
		<div class="container">
		<h3>Friends: </h3>
		<% 
		try {
			List<User> friends = (List<User>) request.getAttribute("friends"); 
			if(!(friends.size() == 0)) {
			for(User u : friends) { %>
			<p>
				<div class="media"><a class="pull-left" href="<% out.print("Profile?id="+ u.getId());%>">
				<img class="media-object" alt="64x64" style="width: 64px; height: 64px;" src="<% out.print(u.getAvatar()); %>" data-src="holder.js/64x64">
				<div class="media-body">		
			    <h4 class="media-heading"><% out.print(u.getNome() + " " + u.getCognome()); %></h4></div></a></div>
		    </p>
		<% } 
		} else { %>
				<div class="alert alert-info"><strong>No friends:</strong> this user has no friends</div>
		<% } } catch (NullPointerException e) { %>
				<div class="alert alert-info"><strong>No friends:</strong> this user has no friends</div>
		<% } %>
		</div>
		<div class="container">
		<h3>Feedbacks received: </h3>
		<%
		try {
			List<HelpRequests> requests = (List<HelpRequests>) request.getAttribute("requests"); 
			if (!(requests.size()==0)) {
				for(HelpRequests r : requests) { %>
					<p><% out.print(r.getFeedback()); %></p>
					<p><i>This feedback was left by <b><a href="Profile?id<% out.print(r.getMittente().getId());%>"><% out.print(r.getMittente().getNome() + " " + r.getMittente().getCognome()); %></a></b> for the ability <% out.println(r.getAbilita().getNome()); %></i></p>
				<% } %>
			<% } else { %>
				<div class="alert alert-info"><strong>No friends:</strong> this user has no feedbacks</div>			
			<% } %>
			<% } catch (NullPointerException e) { %>
				<div class="alert alert-info"><strong>No friends:</strong> this user has no feedbacks</div>
		<% } %>
	  	</div>
	  	</div>
	</div>
</body>
</html>